/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.muni.fi.pa165.library.backend;

import cz.muni.fi.pa165.library.api.BookService;
import cz.muni.fi.pa165.library.api.BookTO;
import cz.muni.fi.pa165.library.api.Genre;
import java.util.List;
import javax.jws.WebService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.security.access.annotation.Secured;

/**
 *
 * @author dulinka
 */
@Service
@Transactional
@WebService(endpointInterface = "cz.muni.fi.pa165.library.api.BookService", serviceName = "BookWebService")
public class BookServiceImpl implements BookService {

    private BookDAO bookDAO;
    private static BookServiceImpl instance = new BookServiceImpl();

    public void setBookDAO(BookDAO bookDAO) {
        this.bookDAO = bookDAO;
    }

    public static BookServiceImpl getInstance() {
        return instance;
    }

    @Secured("ROLE_LIBRARIAN")
    public BookTO createBook(BookTO book) throws IllegalArgumentException {
        return DTOConvertor.convertBookEntityToTO(bookDAO.createBook(DTOConvertor.convertBookTOToEntity(book)));
    }

    @Secured({"ROLE_LIBRARIAN" , "ROLE_READER"})
    public List<BookTO> findAllBooks() {
        return DTOConvertor.convertBookEntityListToTOList(bookDAO.findAllBooks());
    }

    @Secured({"ROLE_LIBRARIAN" , "ROLE_READER"})
    public BookTO findBookById(Long id) throws IllegalArgumentException {
        return DTOConvertor.convertBookEntityToTO(bookDAO.findBookById(id));
    }

    @Secured({"ROLE_LIBRARIAN" , "ROLE_READER"})
    public List<BookTO> findBookByAuthor(String author) throws IllegalArgumentException {
        return DTOConvertor.convertBookEntityListToTOList(bookDAO.findBookByAuthor(author));
    }

    @Secured({"ROLE_LIBRARIAN" , "ROLE_READER"})
    public List<BookTO> findBookByTitle(String title) throws IllegalArgumentException {
        return DTOConvertor.convertBookEntityListToTOList(bookDAO.findBookByTitle(title));
    }

    @Secured({"ROLE_LIBRARIAN" , "ROLE_READER"})
    public List<BookTO> findBookByGenre(Genre genre) throws IllegalArgumentException {
        return DTOConvertor.convertBookEntityListToTOList(bookDAO.findBookByGenre(genre));
    }

    @Secured({"ROLE_LIBRARIAN" , "ROLE_READER"})
    public List<BookTO> findBookByIsbn(String ISBN) throws IllegalArgumentException {
        return DTOConvertor.convertBookEntityListToTOList(bookDAO.findBookByIsbn(ISBN));
    }

    @Secured("ROLE_LIBRARIAN")
    public void updateBook(BookTO book) throws IllegalArgumentException {
        bookDAO.updateBook(DTOConvertor.convertBookTOToEntity(book));
    }

    @Secured("ROLE_LIBRARIAN")
    public void deleteBook(BookTO book) throws IllegalArgumentException {
        bookDAO.deleteBook(DTOConvertor.convertBookTOToEntity(book));
    }

    @Secured({"ROLE_LIBRARIAN" , "ROLE_READER"})
    public List<BookTO> findAllAvailableBooks() {
        return DTOConvertor.convertBookEntityListToTOList(bookDAO.findAllAvailableBooks());
    }
}